﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace HackingSilverlightCodeBrowser
{
    public partial class MyCodeBlock : ChildWindow
    {
        private HSLCollection SourceCodeLibrary = new HSLCollection();
        private int CurrentPosition = 0;

        public MyCodeBlock(string ThisTitle, double ThisWidth )
        {
            InitializeComponent();

            this.Title = ThisTitle;

            TagWrapPanel.Width = ThisWidth - 100;

            for (CurrentPosition = 0; CurrentPosition < SourceCodeLibrary.Collection.Count; CurrentPosition++)
            {
                if (SourceCodeLibrary.Collection[CurrentPosition].Title == ThisTitle)
                {
                    SampleCodeBox.Text = FormatCode(SourceCodeLibrary.Collection[CurrentPosition].Block); 
                    SetTags(SourceCodeLibrary.Collection[CurrentPosition].Tags.Split('~')); 
                    break;
                }
            } 
        }

        private void SetTags(string[] MyTags)
        {
            string Delimiter = string.Empty;

            TagWrapPanel.Children.Clear();

            for (int x = 0; x < MyTags.Length; x++)
            {
                if (Delimiter != string.Empty)
                {
                    TextBlock AComma = new TextBlock();
                    AComma.Text = Delimiter;
                    AComma.Margin = new Thickness(0, 2, 2, 2);
                    TagWrapPanel.Children.Add(AComma);
                }

                TextBlock ATag = new TextBlock();
                ATag.Text = MyTags[x];
                ATag.MouseLeftButtonDown += new MouseButtonEventHandler(ATag_MouseLeftButtonDown);
                ATag.Margin = new Thickness(2, 2, 2, 2);
                ATag.Cursor = Cursors.Hand; 
                TagWrapPanel.Children.Add(ATag);

                Delimiter = ", ";
            }
        }

        void ATag_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ((MainPage)(App.Current.RootVisual)).PresentTagSearch(((TextBlock)sender).Text);
            this.DialogResult = true;
        }

        private string FormatCode(string InputCode)
        {
            InputCode = InputCode.Replace("/>", "/>\n");
            InputCode = InputCode.Replace(";", ";\n");
            InputCode = InputCode.Replace("//", "\n//");
            InputCode = InputCode.Replace(" : ", " : \n");
            InputCode = InputCode.Replace("}", "\n}");
            InputCode = InputCode.Replace("{", "\n{\n");
            InputCode = InputCode.Replace("<Image.", "\n<Image");
            InputCode = InputCode.Replace("><", ">\n<");
            InputCode = InputCode.Replace(">public", ">\n\npublic");
            InputCode = InputCode.Replace("}public", "}\n\npublic");
            InputCode = InputCode.Replace("}set", "}\nset");


            return InputCode;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void BackBtn_Click(object sender, RoutedEventArgs e)
        {
            CurrentPosition--;
            if (CurrentPosition < 0)
            {
                CurrentPosition = SourceCodeLibrary.Collection.Count - 1; 
            }
            LoadCodeBlock();
        }

        private void NextBtn_Click(object sender, RoutedEventArgs e)
        {
            CurrentPosition++;
            if (CurrentPosition >= SourceCodeLibrary.Collection.Count)
            {
                CurrentPosition = 0;
            }
            LoadCodeBlock();
        }

        private void LoadCodeBlock()
        {
            SampleCodeBox.Text = FormatCode(SourceCodeLibrary.Collection[CurrentPosition].Block);
            this.Title = SourceCodeLibrary.Collection[CurrentPosition].Title;  
            SetTags(SourceCodeLibrary.Collection[CurrentPosition].Tags.Split('~'));
        }
    }
}

